package utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.InitialContext;
import javax.sql.DataSource;

public class DbAccess {
	InitialContext ic;

	DataSource ds = null;

	public Connection conn = null;

	public PreparedStatement stmt = null;

	public ResultSet rs = null;

	public void connectDirect(String mydatabase) throws SQLException {
		try {
	        // Load the JDBC driver
	        String driverName = "com.mysql.jdbc.Driver"; // MySQL MM JDBC driver
	        Class.forName(driverName).newInstance();
	    
	        // Create a connection to the database
	        String serverName = "localhost";
	        String url = "jdbc:mysql://" + serverName +  ":3306/" + mydatabase; // a JDBC url
	        String username = "root";
	        String password = "root";
	        conn = DriverManager.getConnection(url, username, password);
	    } catch (ClassNotFoundException e) {
	    	throw new SQLException("Connect Database Error");
	    } catch (SQLException e) {
	    	throw new SQLException("Connect Database Error");
	    } catch (InstantiationException e) {
	    	throw new SQLException("Connect Database Error");
		} catch (IllegalAccessException e) {
			throw new SQLException("Connect Database Error");
		}
	}
	public DbAccess() {
		
	}
	public DbAccess(String database) throws SQLException {
		try {
			ic = new InitialContext();
			ds = (DataSource) ic.lookup("java:comp/env/jdbc/" + database);
			//conn = ds.getConnection();
		} catch (Exception e) {
			e.printStackTrace();
			DBClose();
			throw new SQLException("Connect Database Error");
		}
	}

	public DbAccess(String dbname, boolean b) {
		try {
			ic = new InitialContext();
			ds = (DataSource) ic.lookup("java:comp/env/jdbc/" + dbname);
		} catch (Exception e) {
			e.printStackTrace();
			DBClose();
		}

	}
	public void DBClose() {
		try {
			if (stmt != null) {
				stmt.close();
			}
			if (rs != null) {
				rs.close();
			}
			if (conn != null) {
				conn.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public ResultSet DBExcuteQuery() throws SQLException {
		try {
			rs = stmt.executeQuery();
		} catch (SQLException e) {
//			e.printStackTrace();
			throw new SQLException();
		}
		return rs;

	}

	public int DBUpdate() throws SQLException {
		try {
			return stmt.executeUpdate();
		} catch (Exception e) {
//			e.printStackTrace();
			throw new SQLException();

		}
	}

	public Connection getConn() throws Exception{
			try {
				return ds.getConnection();
			} catch (Exception e) {
				throw e;
			} finally {
			}
			
	}

	public void beginTransaction() throws SQLException {
		try {
			this.conn.setAutoCommit(false);
		} catch (SQLException e) {
			throw new SQLException("Begin transaction error");
		}
		
	}

	public void commit() throws SQLException {
		try {
			this.conn.commit();
		} catch (SQLException e) {
			throw new SQLException("Comit transaction error");
		}
		
	}
	
	public void rollback() {
		try {
			this.conn.rollback();
		} catch (SQLException e) {
			//throw new SQLException("Rolback transaction error");
		}
		
	}
	public void clear() {
		try {
			if (this.stmt!=null) this.stmt.close();
			if (this.rs!=null) this.rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public boolean checkDS() {
		if (this.ds==null) return false;
		else return true;
	}
	
}
